package com.wing.res.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.product.model.response.ProductPlanningTreeVo;
import com.wing.res.dao.ResProBomDao;
import com.wing.res.model.entity.ResProBom;
import com.wing.res.model.request.ChildrenData;
import com.wing.res.model.request.ResProBomAddForm;
import com.wing.res.service.ResProBomService;

import com.wing.web.utils.JwtUtils;

import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * skuBom管理-ServiceImpl
 *
 * @version 1.1.0
 * @Author hz
 * @Date 2024-09-23 20:59
 **/
@Service
public class ResProBomServiceImpl extends ServiceImpl<ResProBomDao, ResProBom> implements ResProBomService {

    /**
     * 添子件bom
     *
     * @param resProBomAddForm
     * @return
     */
    @Override
    public int addBom(ResProBomAddForm resProBomAddForm) {
        if ("1".equals(resProBomAddForm.getFlag())) {
            //查询是否有父id为-1的
            QueryWrapper<ResProBom> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("product_detail_id",resProBomAddForm.getParentId());
            queryWrapper.eq("parent_id","-1");
            queryWrapper.eq("is_deleted","0");
            ResProBom resProBom = this.getOne(queryWrapper);
            Long parentId = null;
            if (ObjectUtil.isEmpty(resProBom)) {
                //加入父bom
                ResProBom proBom = ResProBom.builder()
                        .creator(JwtUtils.getLoginUserId())
                        .creatorName(JwtUtils.getLoginUsername())
                        .createTime(LocalDateTime.now())
                        .updateTime(LocalDateTime.now())
                        .modifier(JwtUtils.getLoginUserId())
                        .modifierName(JwtUtils.getLoginUsername())
                        .productDetailId(resProBomAddForm.getParentId())
                        .skuCode(resProBomAddForm.getParentCode())
                        .parentId(-1L)
                        .isDeleted(false)
                        .isFinal(false)
                        .hasChild(true)
                        .hasEnableChild(false)
                        .build();
                this.save(proBom);
                parentId = proBom.getId();
            }else{
                parentId = resProBom.getId();
            }
            //添加子结构件
            List<ChildrenData> childrenData = resProBomAddForm.getChildrenData();
            Long finalParentId = parentId;
            List<ResProBom> resProBomList = childrenData.stream().map(info -> ResProBom.builder()
                    .creator(JwtUtils.getLoginUserId())
                    .creatorName(JwtUtils.getLoginUsername())
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .modifier(JwtUtils.getLoginUserId())
                    .modifierName(JwtUtils.getLoginUsername())
                    .productDetailId(info.getSkuId())
                    .skuCode(info.getSkuCode())
                    .parentId(finalParentId)
                    .isFinal(info.getIsFinal())
                    .hasChild(false)
                    .hasEnableChild(false)
                    .build()).collect(Collectors.toList());
            this.saveBatch(resProBomList);

        } else {
            //查询
            ResProBom resProBom = this.baseMapper.selectById(resProBomAddForm.getBomId());
            resProBom.setHasChild(true);
            this.updateById(resProBom);

            //添加子结构件
            List<ChildrenData> childrenData = resProBomAddForm.getChildrenData();
            List<ResProBom> resProBomList = childrenData.stream().map(info -> ResProBom.builder()
                    .creator(JwtUtils.getLoginUserId())
                    .creatorName(JwtUtils.getLoginUsername())
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .modifier(JwtUtils.getLoginUserId())
                    .modifierName(JwtUtils.getLoginUsername())
                    .productDetailId(info.getSkuId())
                    .skuCode(info.getSkuCode())
                    .parentId(resProBom.getId())
                    .isFinal(info.getIsFinal())
                    .hasChild(false)
                    .hasEnableChild(false)
                    .build()).collect(Collectors.toList());
            this.saveBatch(resProBomList);
        }
        return 1;
    }

    /**
     * 修改个数和描述
     *
     * @param id
     * @param count
     * @param remark
     * @return
     */
    @Override
    public int updateBom(String id, Integer count, String remark) {
        ResProBom resProBom = this.baseMapper.selectById(id);
        if (ObjectUtil.isNotEmpty(resProBom)) {
            resProBom.setCount(count);
            resProBom.setRemark(remark);
            resProBom.setUpdateTime(LocalDateTime.now());
            resProBom.setModifier(JwtUtils.getLoginUserId());
            resProBom.setModifierName(JwtUtils.getLoginUsername());
            this.updateById(resProBom);
        }
        return 1;
    }

    /**
     * 根据树节点查询sku数据
     *
     * @param skuId
     * @return
     */
    @Override
    public List<ProductPlanningTreeVo> selectBomTreeBySkuId(Long skuId) {
        List<ProductPlanningTreeVo> vos = this.baseMapper.selectBomTreeBySkuId(skuId);
        return vos;
    }

    /**
     * 移除某个物料信息
     * @param id
     * @return
     */
    @Override
    public int deleteBom(String id) {
        ResProBom resProBom = this.baseMapper.selectById(id);
        resProBom.setIsDeleted(true);
        resProBom.setUpdateTime(LocalDateTime.now());
        resProBom.setModifier(JwtUtils.getLoginUserId());
        resProBom.setModifierName(JwtUtils.getLoginUsername());
        return this.baseMapper.updateById(resProBom);
    }
}
